package com.yue.rpc.remoting.transport.netty.server;

import com.yue.rpc.remoting.dto.RpcMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/**
 * @description:
 * @author: zhangqiyue
 * @date: 2023/3/6 17:11
 **/
public class NettyServerHandler extends SimpleChannelInboundHandler<Object> {
    private NettyServerListener listener;
 
    public NettyServerHandler(NettyServerListener listener) {
        this.listener = listener;
    }
 
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        if (msg != null) {
            if (msg instanceof RpcMessage) {
                listener.channelRead(ctx, (RpcMessage) msg);
            }
        }
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,
                                Throwable cause) throws Exception {
        System.out.println("Unexpected exception from downstream.");
    }
 
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) {
        System.out.println("registered: " + ctx.channel().remoteAddress());
    }
}